package one.stand.controller;

import com.jmp.api.admin.dto.DictRequest;
import com.jmp.api.admin.dto.DictResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.model.ResultModel;
import one.stand.model.SysDictValue;
import one.stand.service.sys.SysDictValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "字典管理")
@RestController
@RequestMapping("adminApi/dict")
@Slf4j
public class DictController {

    @Autowired
    private SysDictValueService sysDictValueService;

    @GetMapping("list")
    @SecurityAuthCheck
    public ResultModel<DictResponse> list(@RequestBody DictRequest request) {

        SysDictValueService.Type type = SysDictValueService.Type.valueOf(request.getType());
        List<SysDictValue> dictValues = sysDictValueService.getList(type, SysDictValueService.DEFAULT_COMPANY_KEY);
        List<DictResponse.Dict> list = ConvertUtils.list(dictValues, dict -> DictResponse.Dict.builder()
                .label(dict.getName())
                .value(dict.getValue())
                .build());

        DictResponse response = DictResponse.builder()
                .list(list)
                .build();
        return ResultModel.success(response);
    }
}
